/**
 * Created by beck on 2015/7/1.
 */
var storage = window.localStorage;
var groupid="88888888888888888888888888888888";
var mid;
$(document).ready(function(){
	function GetQueryString(name) {
		   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
		   var r = window.location.search.substr(1).match(reg);
		   if (r!=null) return (r[2]); return null;
		}
	if(GetQueryString("groupid")!=null){
		groupid=GetQueryString("groupid");
	}
	
	storage.setItem("groupid",groupid);

    $("#getCode").click(function(){
        $(this).unbind("click");
        timedCount();
        Login.GetCheckCode();
    });
	var $ph_num = $("#ph-num");
    var $ph_number =$("#ph-number");
	$ph_num.keyup(function(){
        var val = $(this).val();
        if(val ==''){
            $(this).css("color","black");
        }else{
            Login.CheckMobile($(this),val);
        }
    });
    $ph_number.keyup(function(){
        var val = $(this).val();
        if(val ==''){
            $(this).css("color","black");
        }else{
            Login.CheckMobile($(this),val);
        }
    });
});
var c= 60;
var t;
var groupid=storage.groupId;
var mid=storage.mid;
function timedCount(){
    document.getElementById("getCode").innerHTML = c;
    c= c-1;
    if(c === 0){
        document.getElementById("getCode").innerHTML = "获取验证码";
        //$("#getCode").removeAttr("disabled");
        $("#getCode").bind("click");
        c = 60;
        return 0 ;
    }
    t= setTimeout("timedCount()",1000);
}
var Login = $.extend({
     Logining:function(type){
        if(type ==0){
            var ph_num = $("#ph-num").val();
            var psw = $("#psw").val();
        }else{
            var ph_num = $("#ph-number").val();
            var psw = $("#code").val();
        }
        var Post ="mid="+ph_num+"&paswd="+psw+"&groupid="+groupid+"&type="+type;
            $.ajax({
                url:'../api/account/login',
                type:'post',
                data:Post,
                success:function(msg){
                	
                		if(msg.code ==100){
                			var localData =  window.localStorage;
                			localData.setItem("userNumber",msg.info.n0);
                			localData.setItem("userUrl", msg.info.n1);
                			localData.setItem("userName",msg.info.n2);
                			localData.setItem("userSex",msg.info.n3);
                			localData.setItem("userNative",msg.info.n4);
                			localData.setItem("userBirthday",msg.info.n5);
                			localData.setItem("groupId",msg.info.n6.split(",")[0]);
                			localData.setItem("groupNames",msg.info.n7);
                			localData.setItem("currentGroupName",msg.info.n8);
                			localData.setItem("logoUrl",msg.info.n9);
                			localData.setItem("groupJob",msg.info.n10);
                			localData.setItem("messageCount",msg.info.n11);
                			localData.setItem("token",msg.token);
                			window.location = "main.html?mid="+msg.info.n0+"&groupid="+msg.info.n6.split(",")[0];
                		}
                    }
                });
            },
    CheckMobile:function(name,str){
        var rule = /^1\d{10}$/;
        if(rule.test(str)){
        		name.css("color","green");
        	}else{
        		name.css("color","red");
        	}
        },
    GetCheckCode:function(){
        var $ph_num = $("#ph-number").val();
        var Post = "mid="+$ph_num;
        $.ajax({
            url:'../api/account/sendCode',
            
            type:'post',
            data:Post,
            success:function(msg){
                if(msg.code ==103){
                    alert(msg.message);
                }
            }
        });
    }
});

